<template>
  <div>
    <el-dialog
      title="摄像头监控"
      :visible.sync="dialogVisible"
      width="800px"
      @close="close"
    >
      <el-form ref="form" :model="form" :rules="formRules" label-width="130px">
        <el-form-item label="摄像头名称" prop="monitorName">
          <el-input v-model="form.monitorName" clearable :maxlength="100" placeholder="请输入摄像头名称" />
        </el-form-item>
        <el-form-item
          label="摄像头标识"
          prop="cameraMonitorId"
          clearable
          :maxlength="100"
          placeholder="请输入摄像头标识"
        >
          <el-input v-model="form.cameraMonitorId" />
        </el-form-item>
        <el-form-item label="系统名称" prop="systemId">
          <el-select v-model="form.systemId" clearable placeholder="请选择系统名称">
            <el-option
              v-for="(item, index) in systemIdList"
              :key="index"
              :label="item.systemCode + ' / ' + item.systemName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备厂商" prop="monitorFactory" clearable :maxlength="100" placeholder="请输入设备厂商">
          <el-select v-model="form.monitorFactory" clearable placeholder="请选择设备厂商">
            <el-option
              v-for="(item, index) in dictOptions.monitorFactory"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备ip" prop="monitorIp" clearable :maxlength="100" placeholder="请输入设备ip">
          <el-input v-model="form.monitorIp" />
        </el-form-item>
        <el-form-item label="端口号" prop="monitorPort" clearable :maxlength="100" placeholder="请输入端口号">
          <el-input v-model="form.monitorPort" />
        </el-form-item>
        <el-form-item label="用户名" prop="monitorUsername" clearable :maxlength="100" placeholder="请输入用户名">
          <el-input v-model="form.monitorUsername" />
        </el-form-item>
        <el-form-item label="密码" prop="monitorPassword" clearable :maxlength="100" placeholder="请输入密码">
          <el-input v-model="form.monitorPassword" show-password />
        </el-form-item>
        <el-form-item label="CRON表达式" prop="cron">
          <el-input v-model="form.cron" clearable disabled :maxlength="100" placeholder="请输入CRON表达式">
            <el-button slot="append" @click="$refs.vcrontabRef.showDialog()"> 生成表达式</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="预警通知人" prop="alarmUsers">
          <el-select
            v-model="form.alarmUsers"
            multiple
            collapse-tags
            placeholder="请选择"
            collapse-tag
            filterable
            clearable
          >
            <el-option v-for="(item) in receiveUserList" :key="item.id" :value="item.id" :label="item.realName" />
          </el-select>
        </el-form-item>

        <el-form-item label="摄像头在线状态">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="在线"
            active-value="1"
            inactive-text="离线"
            inactive-value="0"
            disabled
          />
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.taskStatus"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="开启"
            active-value="1"
            inactive-text="停止"
            inactive-value="2"
            :disabled="disabled"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remarks" clearable placeholder="请输入备注">
          <el-input v-model="form.remarks" :row="5" :maxlength="200" type="textarea" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="connect">测试连接</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="interfaceServiceConfirm()">确 定</el-button>
      </span>
    </el-dialog>
    <dt-Vcrontab ref="vcrontabRef" v-model.trim="form.cron" />
  </div>
</template>

<script>
import { getSystemListApi } from '@/api/serviceManage/requestPartyServiceManage'
import { addApi, connectApi, editApi } from '@/api/cameraMonitor/cameraMonitor'
import dictMixin from '@/utils/mixins/dict'
import { listMonitorAlarmUser } from '@/api/contactAlarm'
import { addRes } from '@/utils/editAlarmUser'

export default {
  name: 'AddEditDataWatch',
  mixins: [
    dictMixin(['monitorFactory@Local'])
  ],
  data() {
    return {
      disabled: true,
      canClick: true,
      systemIdList: [],
      dialogVisible: false,
      form: {
        id: null,
        systemId: null,
        monitorName: null,
        monitorFactory: null,
        cameraMonitorId: null,
        monitorIp: null,
        monitorPort: null,
        monitorUsername: null,
        monitorPassword: null,
        cron: null,
        remarks: null,
        delFlag: null,
        taskStatus: '1',
        status: null
      },
      formRules: {
        systemId: { required: true, message: '请选择系统', trigger: 'blur' },
        monitorName: { required: true, message: '请填写设备名称', trigger: 'blur' },
        monitorFactory: { required: true, message: '请选择设备厂商', trigger: 'blur' },
        cameraMonitorId: { required: true, message: '请填设备编码', trigger: 'blur' },
        monitorIp: { required: true, message: '请填写设备IP', trigger: 'blur' },
        monitorPort: { required: true, message: '请填写设备端口', trigger: 'blur' },
        monitorUsername: { required: true, message: '请填写用户名', trigger: 'blur' },
        monitorPassword: { required: true, message: '请填写密码', trigger: 'blur' },
        cron: { required: true, message: '请生成CRON表达式', trigger: 'change' },
        alarmUsers: { required: true, message: '请选择预警通知人', trigger: 'blur' }
      },
      receiveUserList: []
    }
  },
  created() {
    listMonitorAlarmUser({ pageNo: 1, pageSize: -1 }).then(res => {
      this.receiveUserList = res.data.rows
    })
  },
  methods: {
    close() {
      this.$refs.form.resetFields()
    },
    getInitData(row) {
      // 获取系统
      getSystemListApi()
        .then(({ code, data }) => {
          if (code === '00000') {
            this.systemIdList = data
          }
        })
    },
    // 确定按钮
    interfaceServiceConfirm() {
      this.$refs['form'].validate((valid) => {
        const data = JSON.parse(JSON.stringify(this.form))
        data.alarmUsers = addRes(data.alarmUsers)

        if (valid) {
          if (this.form.id) {
            editApi(data).then(({ code, data }) => {
              if (code === '00000') {
                this.dialogVisible = false
                this.canClick = true
                this.$emit('interfaceServiceRefresh')
              }
            })
          } else {
            addApi(data).then(({ code, data }) => {
              if (code === '00000') {
                this.dialogVisible = false
                this.canClick = true
                this.$emit('interfaceServiceRefresh')
              }
            })
          }
        }
      })
    },
    connect() {
      const data = JSON.parse(JSON.stringify(this.form))
      data.alarmUsers = addRes(data.alarmUsers)

      this.$refs['form'].validate((valid) => {
        if (valid) {
          connectApi(data)
            .then(({ code, data }) => {
              if (code === '00000') {
                this.canClick = false
                // 提示联通成功
                this.$message.success('联通成功')
                this.form.status = data ? '1' : '0'
              }
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.create-service {
  ::v-deep .el-dialog__body {
    padding: 0 20px;
    height: 500px;
    overflow-y: auto;
  }
}
</style>

